#include("../../layout/header.html")
#@layout()
#define content()
<style>
  .formAdvForm .layui-form-item {
    margin-top: 20px;
    margin-bottom: 0;
  }

  .formAdvForm .layui-form-item .layui-inline {
    margin-bottom: 25px;
    margin-right: 0;
  }

  .form-group-bottom {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    padding: 10px 20px;
    background-color: #fff;
    box-shadow: 0 -1px 2px 0 rgba(0, 0, 0, .05);
  }
</style>
<body>
<!-- 正文开始 -->
<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-card-body">
      <div class="layui-tab layui-tab-brief" lay-filter="demo">
        <ul class="layui-tab-title">
          <li>基础信息</li>
          <li class="layui-this">字段信息</li>
        </ul>
        <div class="layui-tab-content">
          <div class="layui-tab-item" style="padding-top: 20px;">
            <div class="layui-row layui-col-space30" id="showlist1" ></div>
          </div>
          <div class="layui-tab-item layui-show" style="padding-top: 20px;">
            <div class="layui-row layui-col-space30" id="showlist2">
            </div>
          </div>



        </div>
      </div>
      <div class="form-group-bottom text-right">
        <button class="layui-btn" lay-filter="formAdvSubmit1" lay-submit><i class="layui-icon layui-icon-ok"></i>&emsp;提交&emsp;</button>
        <button type="reset" class="layui-btn layui-btn-primary"><i class="layui-icon layui-icon-refresh"></i>&emsp;重置&emsp;</button>
      </div>

    </div>
  </div>
</div>

<!--基础信息-->
<script type="text/html" id="list1">
  <form id="genTableForm1"  lay-filter="genTableForm1" class="layui-form toolbar formAdvForm">
    <input name="tableId" type="hidden">
    <div class="layui-form-item layui-row">
      <div class="layui-inline layui-col-md6">
        <label class="layui-form-label layui-form-required">表名：</label>
        <div class="layui-input-block">
          <input name="tableName" type="text" class="layui-input" placeholder="请输入表名称">
        </div>
      </div>

      <div class="layui-inline layui-col-md6">
        <label class="layui-form-label layui-form-required">描述：</label>
        <div class="layui-input-block">
          <input name="tableComment" type="text" class="layui-input" placeholder="请输入描述内容">
        </div>
      </div>
    </div>

    <div class="layui-form-item layui-row">
      <div class="layui-inline layui-col-md6">
        <label class="layui-form-label layui-form-required">实体类：</label>
        <div class="layui-input-block">
          <input name="className" type="text" class="layui-input" placeholder="请输入实体类名称">
        </div>
      </div>

      <div class="layui-inline layui-col-md6">
        <label class="layui-form-label layui-form-required">作者：</label>
        <div class="layui-input-block">
          <input name="functionAuthor" type="text" class="layui-input" placeholder="请输入作者名称">
        </div>
      </div>
    </div>

    <div class="layui-form-item layui-row">
      <div class="layui-inline layui-col-md6">
        <label class="layui-form-label layui-form-required">生成模板:</label>
        <div class="layui-input-block">
          <select name="tplCategory" id="tplCategory" lay-filter="tplCategory">
            <option></option>
            <option value="crud">单表（增删改查）</option>
            <option value="tree" disabled>树表（增删改查）</option>
            <option value="sub" disabled>主子表（增删改查）</option>
          </select>
        </div>
      </div>
      <div class="layui-inline layui-col-md6">
        <label class="layui-form-label layui-form-required">包路径:</label>
        <div class="layui-input-block">
          <input name="packageName" placeholder="请输入包路径" class="layui-input"
                 lay-verType="tips" lay-verify="required" required/>
        </div>
      </div>
      <!--<div class="layui-inline layui-col-md6">
        <label class="layui-form-label layui-form-required">上级菜单:</label>
        <div class="layui-input-block">
          <div id="parentMenuId" class="ew-xmselect-tree"></div>
          <select name="parentMenuId" lay-verType="tips" lay-verify="required" required>
            <option value="">请选择仓库类型</option>
            <option value="0">私密</option>
            <option value="1">公开</option>
          </select>
        </div>
      </div>-->
    </div>

    <div class="layui-form-item layui-row">
      <div class="layui-inline layui-col-md6">
        <label class="layui-form-label layui-form-required">模块名称:</label>
        <div class="layui-input-block">
          <input name="moduleName" placeholder="请输入模块名称" class="layui-input" lay-verType="tips" lay-verify="required" required/>
        </div>
      </div>
      <div class="layui-inline layui-col-md6">
        <label class="layui-form-label layui-form-required">业务名称:</label>
        <div class="layui-input-block">
          <input name="businessName" placeholder="请输入业务名称" class="layui-input" lay-verType="tips" lay-verify="required" required/>
        </div>
      </div>
    </div>

    <div class="layui-form-item layui-row">
      <div class="layui-inline layui-col-md6">
        <label class="layui-form-label layui-form-required">功能名称:</label>
        <div class="layui-input-block">
          <input name="functionName" placeholder="请输入功能名称" class="layui-input" lay-verType="tips" lay-verify="required" required/>

        </div>
      </div>
    </div>

    <div class="layui-form-item">
      <label class="layui-form-label">生成方式:</label>
      <div class="layui-input-block">
        <input type="radio" name="genType" value="0" title="zip压缩包">
        <input type="radio" name="genType" value="1" title="自定义路径">
      </div>
    </div>

    <div class="layui-form-item layui-form-text">
      <label class="layui-form-label">生成路径</label>
      <div class="layui-input-block">
        <input name="genPath" type="text" class="layui-input">
      </div>
    </div>

    <div class="layui-form-item layui-form-text">
      <label class="layui-form-label">备注：</label>
      <div class="layui-input-block">
        <textarea name="remark" placeholder="请输入内容" class="layui-textarea"></textarea>
      </div>
    </div>

    <div class="layui-form-item layui-hide">
      <div class="layui-input-block">
        <button id="formDemo" class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
      </div>
    </div>
  </form>

</script>
<!--字段信息-->
<script type="text/html" id="list2">
    <!-- 数据表格 -->
    <table id="dataTable" lay-filter="dataTable"></table>
</script>



<script type="text/html" id="javaTypeTpl">
  <div class="ew-select-fixed">
    <select lay-filter="javaType" data-id="{{d.columnId}}">
      <option value="">请选择</option>
      <option value="Long" {{d.javaType=='Long'?'selected':''}}>Long</option>
      <option value="String" {{d.javaType=='String'?'selected':''}}>String</option>
      <option value="Integer" {{d.javaType=='Integer'?'selected':''}}>Integer</option>
      <option value="Double" {{d.javaType=='Double'?'selected':''}}>Double</option>
      <option value="BigDecimal" {{d.javaType=='BigDecimal'?'selected':''}}>BigDecimal</option>
      <option value="Date" {{d.javaType=='Date'?'selected':''}}>Date</option>
    </select>
  </div>
</script>
<script type="text/html" id="queryTypeTpl">
  <div class="ew-select-fixed">
    <select lay-filter="queryType" data-id="{{d.columnId}}">
      <option value="">请选择</option>
      <option value="EQ" {{d.queryType=='EQ'?'selected':''}}>=</option>
      <option value="NE" {{d.queryType=='NE'?'selected':''}}>!=</option>
      <option value="GT" {{d.queryType=='GT'?'selected':''}}>></option>
      <option value="GTE" {{d.queryType=='GTE'?'selected':''}}>>=</option>
      <option value="LT" {{d.queryType=='LT'?'selected':''}}><</option>
      <option value="LTE" {{d.queryType=='LTE'?'selected':''}}><=</option>
      <option value="LIKE" {{d.queryType=='LIKE'?'selected':''}}>Like</option>
      <option value="BETWEEN" {{d.queryType=='BETWEEN'?'selected':''}}>Between</option>
    </select>
  </div>
</script>
<script type="text/html" id="htmlTypeTpl">
  <div class="ew-select-fixed">
    <select lay-filter="htmlType" data-id="{{d.columnId}}">
        <option value="">请选择</option>
        <option value="input" {{d.htmlType=='input'?'selected':''}}>文本框</option>
        <option value="input_number" {{d.htmlType=='input_number'?'selected':''}}>数字框</option>
        <option value="input_pass" {{d.htmlType=='input_pass'?'selected':''}}>密码框</option>
        <option value="textarea" {{d.htmlType=='textarea'?'selected':''}}>文本域</option>
        <option value="select" {{d.htmlType=='select'?'selected':''}}>下拉框</option>
        <option value="radio" {{d.htmlType=='radio'?'selected':''}}>单选框</option>
        <option value="checkbox" {{d.htmlType=='checkbox'?'selected':''}}>复选框</option>
        <option value="switch" {{d.htmlType=='switch'?'selected':''}}>开关</option>
        <option value="summernote" {{d.htmlType=='summernote'?'selected':''}}>富文本</option>
        <option value="datetime" {{d.htmlType=='datetime'?'selected':''}}>日期控件</option>
        <option value="upload" {{d.htmlType=='upload'?'selected':''}}>上传控件</option>
    </select>
  </div>
</script>
<script type="text/html" id="isRequiredTpl">
  <input type="checkbox" name="isRequired" data-id="{{d.columnId}}" lay-skin="primary" value="{{d.isRequired}}" lay-filter="isRequired" {{ d.isRequired == 1 ? 'checked' : '' }}>
</script>
<script type="text/html" id="isInsertTpl">
  <input type="checkbox" name="isInsert" data-id="{{d.columnId}}" lay-skin="primary" value="{{d.isInsert}}" lay-filter="isInsert" {{ d.isInsert == 1 ? 'checked' : '' }}>
</script>
<script type="text/html" id="isEditTpl">
  <input type="checkbox" name="isEdit" data-id="{{d.columnId}}" lay-skin="primary" value="{{d.isEdit}}" lay-filter="isEdit" {{ d.isEdit == 1 ? 'checked' : '' }}>
</script>
<script type="text/html" id="isListTpl">
  <input type="checkbox" name="isList" data-id="{{d.columnId}}" lay-skin="primary" value="{{d.isList}}" lay-filter="isList" {{ d.isList == 1 ? 'checked' : '' }}>
</script>
<script type="text/html" id="isQueryTpl">
  <input type="checkbox" name="isQuery" data-id="{{d.columnId}}" lay-skin="primary" value="{{d.isQuery}}" lay-filter="isQuery" {{ d.isQuery == 1 ? 'checked' : '' }}>
</script>

<script type="text/html" id="gridTpl">
  <div class="ew-select-fixed">
    <select lay-filter="gridType" data-id="{{d.columnId}}">
      <option value="">请选择</option>
      <option value="121" {{d.gridType=='121'?'selected':''}}>一列/12栅格</option>
      <option value="122" {{d.gridType=='122'?'selected':''}}>两列/12栅格</option>
      <option value="123" {{d.gridType=='123'?'selected':''}}>三列/12栅格</option>
      <option value="124" {{d.gridType=='124'?'selected':''}}>四列/12栅格</option>
    </select>
  </div>
</script>

<script type="text/html" id="dictTypeTpl">
  <div class="ew-select-fixed" data-id="{{d.columnId}}">
    <select id="dictType" lay-filter="dictType"></select>
  </div>
</script>

<!-- js部分 -->
<script>
  layui.use(['layer', 'form', 'table', 'util', 'admin','formX','element'], function () {
    var $ = layui.jquery;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var util = layui.util;
    var formX = layui.formX;
    var admin = layui.admin;
    var element = layui.element;
    var xmSelect = layui.xmSelect;
    var iconPicker = layui.iconPicker;
    var tableId = admin.getLayerData().tableId;
    var dictData = [],curdData = [
      {name:'插入',value:'insert'},
      {name:'编辑',value:'edit'},
      {name:'列表',value:'list'},
      {name:'查询',value:'query'},
    ];

    var list1 = document.getElementById('list1').innerHTML;
    var list2 = document.getElementById('list2').innerHTML;
    document.getElementById('showlist1').innerHTML = list1;
    document.getElementById('showlist2').innerHTML = list2;
    form.render();

    /*字典获取接口*/
    admin.req('/sys/sysDict/getAll',{},function (d) {
      if (0 === d.code) {
        dictData = d.data;
        // formX.renderSelect({elem: '#dictType', data: dictData, name: 'name', value: 'code'});
      }
    })


    /* 渲染表格 */
    var insTb = table.render({
      elem: '#dataTable',
      url: '/gen/genTable/tableColumnList?tableId=' + tableId,
      page: true,
      cellMinWidth: 100,
      limit:50,
      cols: [[
        {align: 'center', title: '字段', colspan: 6},
        {align: 'center', title: '表单', colspan: 10}
      ],[
        {type: 'checkbox',hide:true,LAY_CHECKED:true},
        {type: 'numbers'},
        {field: 'columnName', title: '字段名', sort: true},
        {field: 'columnComment', title: '描述', edit: 'text'},
        {field: 'columnType', title: 'DB类型', sort: true},
        {field: 'javaType', title: 'JAVA类型', templet: '#javaTypeTpl'},
        {field: 'javaField', title: 'JAVA字段', edit: 'text'},
        {field: 'isInsert', title: '插入',width:60, templet: '#isInsertTpl'},
        {field: 'isEdit', title: '编辑',width:60, templet: '#isEditTpl'},
        {field: 'isList', title: '列表',width:60, templet: '#isListTpl'},
        {field: 'isQuery', title: '查询',width:60, templet: '#isQueryTpl'},
        {field: 'queryType', title: '查询方式', templet: '#queryTypeTpl'},
        {field: 'isRequired', title: '必填', templet: '#isRequiredTpl'},
        {field: 'htmlType', title: '显示类型', templet: '#htmlTypeTpl'},
        {field: 'dictType', title: '字典类型', templet: function (d) {
            let htmlStart = '<div class="ew-select-fixed"><select name="dictType" id="dictType" lay-filter="dictType" data-id="'+d.columnId+'" lay-search><option></option>'
            dictData.map(function (item) {
              if(d.dictType === item.code){
                htmlStart += '<option value="'+item.code+'" selected>'+item.name+'</option>';
              }else {
                htmlStart += '<option value="'+item.code+'">'+item.name+'</option>';
              }

            });
            htmlStart += '</select></div>';
            return htmlStart;
          }},
        {field: 'grid', title: '栅格',  templet: '#gridTpl'},
      ]],
      done:function (res) {
        form.render('select');
      }
    });

    /*获取生成相关信息*/
    admin.req('/gen/genTable/info',{id:tableId},function (d) {
      if(d.code === 0){
        // console.log(d.data);
        formX.val('genTableForm1',d.data);
        /*admin.req('/sys/sysAuthorities/list',{page:1,limit:10000},function (dd) {
          if(dd.code === 0){
            formX.renderTreeSelect({el:'#parentMenuId',name:'parentMenuId',data:dd.data});
          }
        })*/

        formX.val('genTableForm3',d.data);
      }
    })

    /* 表格头工具栏点击事件 */
    table.on('toolbar(dataTable)', function (obj) {
      if (obj.event === 'add') { // 添加
        var checkRows = table.checkStatus('dataTable');
        if (checkRows.data.length === 0) {
          layer.msg('请选择要添加的数据', {icon: 2});
          return;
        }
        var tableNames = checkRows.data.map(function (d) {
          return d.tableName;
        });
        console.log(tableNames)
        var loadIndex = layer.load(2);
        admin.req('/gen/genTable/import?tableNames=' + tableNames,{},function (d) {
          if (0 === d.code) {
            layer.close(loadIndex);
            layer.msg(d.msg, {icon: 1});
            window.parent.insTb.reload({page: {curr: 1}});
          }else {
            layer.msg(d.msg, {icon: 2});
          }
        })
        return false;
      }
    });

    /* 表格搜索 */
    form.on('submit(dataTbSearch)', function (data) {
      insTb.reload({where: data.field, page: {curr: 1}});
      return false;
    });

    form.on('submit(formAdvSubmit1)', function (data) {
      $('#formDemo').click();
      return false;
    });

    form.on('submit(formDemo)', function (data) {
      var loadIndex = layer.load(2);
      admin.post('/gen/genTable/add',data.field,function (d) {
        layer.close(loadIndex);
        if(d.code === 0){
          layer.close(loadIndex);
          layer.msg(d.msg, {icon: 1});
          insTb.reload({page: {curr: 1}});
        }else {
          layer.msg(d.msg, {icon: 2});
        }
      })
      return false;
    })

    form.on('select(javaType)', function(data){
      admin.post('/gen/genTable/eidtTableColumn',{columnId:data.elem.getAttribute('data-id'),javaType:data.value})
    });

    form.on('select(queryType)', function(data){
      admin.post('/gen/genTable/eidtTableColumn',{columnId:data.elem.getAttribute('data-id'),queryType:data.value})
    });

    form.on('select(htmlType)', function(data){
      admin.post('/gen/genTable/eidtTableColumn',{columnId:data.elem.getAttribute('data-id'),htmlType:data.value})
    });

    form.on('select(gridType)', function(data){
      admin.post('/gen/genTable/eidtTableColumn',{columnId:data.elem.getAttribute('data-id'),gridType:data.value})
    });

    form.on('select(dictType)', function(data){
      admin.post('/gen/genTable/eidtTableColumn',{columnId:data.elem.getAttribute('data-id'),dictType:data.value})
    });

    form.on('checkbox(isRequired)', function(data){
      admin.post('/gen/genTable/eidtTableColumn',{columnId:data.elem.getAttribute('data-id'),isRequired:data.elem.checked ? 1 : 0})
    });
    form.on('checkbox(isInsert)', function(data){
      admin.post('/gen/genTable/eidtTableColumn',{columnId:data.elem.getAttribute('data-id'),isInsert:data.elem.checked ? 1 : 0})
    });
    form.on('checkbox(isEdit)', function(data){
      admin.post('/gen/genTable/eidtTableColumn',{columnId:data.elem.getAttribute('data-id'),isEdit:data.elem.checked ? 1 : 0})
    });
    form.on('checkbox(isList)', function(data){
      admin.post('/gen/genTable/eidtTableColumn',{columnId:data.elem.getAttribute('data-id'),isList:data.elem.checked ? 1 : 0})
    });
    form.on('checkbox(isQuery)', function(data){
      admin.post('/gen/genTable/eidtTableColumn',{columnId:data.elem.getAttribute('data-id'),isQuery:data.elem.checked ? 1 : 0})
    });
    table.on('edit(dataTable)', function(obj){
      var value = obj.value //得到修改后的值
              ,data = obj.data //得到所在行所有键值
              ,field = obj.field; //得到字段
      // console.log('[ID: '+ data.columnId +'] ' + field + ' 字段更改值为：'+ util.escape(value));
      admin.post('/gen/genTable/eidtTableColumn',{columnId:data.columnId,[field]:util.escape(value)})
    });


  });
</script>
</body>
#end
